package eu.expressis.eiaf.db.extract;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatDtdDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;

public class DatabaseExportSample {
  public static void main(String[] args) throws Exception {
    // database connection
    // Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
    Connection jdbcConnection = DriverManager.getConnection("jdbc:hsqldb:file:D:/opt/db/eiaf", "sa", "");
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

    // write DTD file
    FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream("test.dtd"));

    // database connection

    // // partial database export
    // QueryDataSet partialDataSet = new QueryDataSet(connection);
    // partialDataSet.addTable("FOO", "SELECT * FROM ADMIN");
    // partialDataSet.addTable("ADMIN");
    // FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial.xml"));

    // full database export
    IDataSet fullDataSet = connection.createDataSet();
    FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));

    // IDataSetProducer producer = new FlatXmlProducer(new InputSource("dataset.xml"));
    // IDataSet dataSet = new StreamingDataSet(producer);

    // dependent tables database export: export table X and all tables that
    // have a PK which is a FK on X, in the right order for insertion
    // String[] depTableNames = TablesDependencyHelper.getAllDependentTables(connection, "USER");
    // IDataSet depDataSet = connection.createDataSet(depTableNames);
    // FlatXmlDataSet.write(depDataSet, new FileOutputStream("dependents.xml"));

  }
}